/**
 * 向触发器关联人员发送消息
 */
def sendTriggerMessage(txt, receiver=null){
	
	try {
		
		if(snail.common.utils.StringHelper.isBlank(receiver)) {
			
			com.seeyon.apps.snail.common.MessageHepler.send(txt, [__TRIGGER_CONTEXT__.event.userId] as long[]);
			
		}else {
			sendMessage(txt, receiver);
		}
		
	} catch(Throwable e){
		
		logger("向触发器关联人员发送消息失败！[TXT: ${txt}, ERROR: ${e.message}]");
	}
}


/* 从【触发器】的环境对象中获取表单数据，并转换为时间 */
def getFieldValueForTime(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getFieldValue(key, null, triggerActionContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
						
			(0..<value.size()).each{i->
				
				try {
					
					if(value[i] instanceof String){
						value[i] = new java.sql.Timestamp(snail.common.utils.DateHelper.parse(value[i], 'yyyy-MM-dd HH:mm:ss').getTime());
					}
				} catch(Exception e){}
			}
						
		} else if(value instanceof String){
			value = new java.sql.Timestamp(snail.common.utils.DateHelper.parse(value, 'yyyy-MM-dd HH:mm:ss').getTime());
		}

	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为时间文本 */
def getFieldValueForTimeText(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getFieldValue(key, null, triggerActionContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
						
			(0..<value.size()).each{i->
				try {
					value[i] = ((null != value[i]) ? (new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value[i])) : '');
				} catch(Exception e){}
			}
						
		} else if(value instanceof java.util.Date){
			value = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
		}

	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为日期 */
def getFieldValueForDate(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getFieldValue(key, null, triggerActionContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			(0..<value.size()).each{i->
				
				try {
					
					if(value[i] instanceof String){
						value[i] = snail.common.utils.DateHelper.parse(value[i]);
					}
				} catch(Exception e){}
			}
						
		} else if(value instanceof String){
			value = snail.common.utils.DateHelper.parse(value);
		}

	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为日期文本 */
def getFieldValueForDateText(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    def value = null;
    
	try {
		
		value = getFieldValue(key, null, triggerActionContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
						
			(0..<value.size()).each{i->
				try {
					value[i] = ((null != value[i]) ? (new java.text.SimpleDateFormat("yyyy-MM-dd").format(value[i])) : '');
				} catch(Exception e){}
			}
						
		} else if(value instanceof java.util.Date){
			value = new java.text.SimpleDateFormat("yyyy-MM-dd").format(value);
		}

	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return value;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为枚举显示值 */
def getFieldValueForEnumShowvalue(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    try {
        
        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getFieldValue(key, defaultValue, triggerActionContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                
                    names << ((null != enumItem) ? enumItem.getShowvalue() : '');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getShowvalue() : defaultValue);    
        }

    } catch(Exception e){
        logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为枚举值 */
def getFieldValueForEnumValue(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    try {

        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getFieldValue(key, defaultValue, triggerActionContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
                
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                    
                    names << ((null != enumItem) ? enumItem.getEnumvalue() : '');
                } catch(Exception e){
                    names << '';
                }
                
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getEnumvalue() : defaultValue);    
        }
        
    } catch(Exception e){
        logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为枚举编码 */
def getFieldValueForEnumCode(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    try {

        def enumManager = com.seeyon.ctp.common.AppContext.getBean("enumManagerNew");
        
        def id = getFieldValue(key, defaultValue, triggerActionContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
                
                try {
                    def enumItem = enumManager.getEnumItem(new Long(id[i] as String));
                    
                    names << ((null != enumItem) ? enumItem.getEnumItemCode() : '');
                } catch(Exception e){
                    names << '';
                }
                
            }
            
            return names;
            
        } else {
            def enumItem = enumManager.getEnumItem(new Long(id as String));
            
            return ((null != enumItem) ? enumItem.getEnumItemCode() : defaultValue);    
        }
        
    } catch(Exception e){
        logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为姓名 */
def getFieldValueForMember(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getFieldValue(key, defaultValue, triggerActionContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _names = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _names << ((null != member) ? member.name : '');
                        } catch(Exception e){}
                    }
                    
                    names << _names.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.name : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为人员编号 */
def getFieldValueForMemberCode(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getFieldValue(key, defaultValue, triggerActionContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.code : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.code : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为人员登录名 */
def getFieldValueForMemberLoginName(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    try {

        def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
        
        def id = getFieldValue(key, defaultValue, triggerActionContext);
        
        if(null == id){
            return defaultValue;
        }
        
        if(id instanceof java.util.List){
            
            def names = [];
            
            (0..<id.size()).each{i->
            
                def _values = [];
                
                try {
                    
                    id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
                    
                    id[i].each{
                        
                        try {
                            def member = orgManager.getMemberById(new Long(it));
                        
                            _values << ((null != member) ? member.loginName : '');
                        } catch(Exception e){}
                    }
                    
                    names << _values.join(',');
                } catch(Exception e){
                    names << '';
                }
            }
            
            return names;
        } else {
            
            def names = [];
            
            id = (id as String).split(',');
            
            id.each{
                
                def member = orgManager.getMemberById(new Long(it));
                
                names << ((null != member) ? member.loginName : '');
            }
            
            return names.join(',');
        }
        
    } catch(Exception e){
        logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}

/* 从【触发器】的环境对象中获取表单数据，并转换为部门名称 */
def getFieldValueForDeptName(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getFieldValue(key, defaultValue, triggerActionContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def department = orgManager.getDepartmentById(new Long(it));
						
							_names << ((null != department) ? department.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def department = orgManager.getDepartmentById(new Long(it));
				
				names << ((null != department) ? department.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
    
    return defaultValue;
}

/* 从【触发器】的环境对象中获取表单数据，并转换为部门编号 */
def getFieldValueForDeptCode(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getFieldValue(key, defaultValue, triggerActionContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def department = orgManager.getDepartmentById(new Long(it));
						
							_values << ((null != department) ? department.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def department = orgManager.getDepartmentById(new Long(it));
				
				names << ((null != department) ? department.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
    
    return defaultValue;
}

/* 从【触发器】的环境对象中获取表单数据，并转换为单位名称 */
def getFieldValueForUnitName(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getFieldValue(key, defaultValue, triggerActionContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def unit = orgManager.getAccountById(new Long(it));
						
							_names << ((null != unit) ? unit.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def unit = orgManager.getAccountById(new Long(it));
				
				names << ((null != unit) ? unit.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器】的环境对象中获取表单数据，并转换为单位编号 */
def getFieldValueForUnitCode(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getFieldValue(key, defaultValue, triggerActionContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def unit = orgManager.getAccountById(new Long(it));
						
							_values << ((null != unit) ? unit.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def unit = orgManager.getAccountById(new Long(it));
				
				names << ((null != unit) ? unit.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为岗位名称 */
def getFieldValueForPostName(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def post = orgManager.getPostById(new Long(it));
						
							_names << ((null != post) ? post.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def post = orgManager.getPostById(new Long(it));
				
				names << ((null != post) ? post.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为岗位编号 */
def getFieldValueForPostCode(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def post = orgManager.getPostById(new Long(it));
						
							_values << ((null != post) ? post.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def post = orgManager.getPostById(new Long(it));
				
				names << ((null != post) ? post.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为职务级别名称 */
def getFieldValueForLevelName(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _names = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def level = orgManager.getLevelById(new Long(it));
						
							_names << ((null != level) ? level.name : '');
						} catch(Exception e){}
					}
					
					names << _names.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def level = orgManager.getLevelById(new Long(it));
				
				names << ((null != level) ? level.name : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}

/* 从【触发器/流程事件】的环境对象中获取表单数据，并转换为职务级别编号 */
def getFieldValueForLevelCode(key, defaultValue=null, triggerDataContext=__TRIGGER_CONTEXT__){

	try {

		def orgManager = com.seeyon.ctp.common.AppContext.getBean("orgManager");
		
		def id = getValue(key, defaultValue, triggerDataContext);
		
		if(null == id){
			return defaultValue;
		}
		
		if(id instanceof java.util.List){
			
			def names = [];
			
			(0..<id.size()).each{i->
			
				def _values = [];
				
				try {
					
					id[i] = ((null != id[i]) ? (id[i] as String).split(',') : '');
					
					id[i].each{
						
						try {
							def level = orgManager.getLevelById(new Long(it));
						
							_values << ((null != level) ? level.code : '');
						} catch(Exception e){}
					}
					
					names << _values.join(',');
				} catch(Exception e){
					names << '';
				}
			}
			
			return names;
		} else {
			
			def names = [];
			
			id = (id as String).split(',');
			
			id.each{
				
				def level = orgManager.getLevelById(new Long(it));
				
				names << ((null != level) ? level.code : '');
			}
			
			return names.join(',');
		}
		
	} catch(Exception e){
		logger("从【${__IS_TRIGGER__ ? '触发器': '流程事件'}】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为数字 */
def getFieldValueForNumber(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){

	try {

		def value = getFieldValue(key, defaultValue, triggerActionContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			def values = [];
			
			value.each{val->
				
				if(null == val){
				
					values << null;
					
				}else if(val instanceof java.lang.Number){
				
					values << val;
				
				} else {
				
					values << new java.math.BigDecimal(val as String);
				}
			}
			
			return values;
		} else if(value instanceof java.lang.Number){
			
			return value;
			
		} else {
			
			return new java.math.BigDecimal(value as String);
		}
		
	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【触发器】的环境对象中获取表单数据，并转换为字符串 */
def getFieldValueForString(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){

	try {

		def value = getFieldValue(key, defaultValue, triggerActionContext);
		
		if(null == value){
			return defaultValue;
		}
		
		if(value instanceof java.util.List){
			
			def values = [];
			
			value.each{val->
				
				if(null == val){
				
					values << null;
					
				}else if(val instanceof java.lang.String){
				
					values << val;
				
				} else {
				
					values << (val as String);
				}
			}
			
			return values;
		} else if(value instanceof java.lang.String){
			
			return value;
			
		} else {
			
			return value as String;
		}
		
	} catch(Exception e){
		logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
	}
	
    return defaultValue;
}


/* 从【触发器】的环境对象中获取表单数据 */
def getFieldValue(key, defaultValue=null, triggerActionContext=__TRIGGER_CONTEXT__){
    
    try {
        def formData = triggerActionContext.getMasterBean();
    
        if(!(key ==~ /field\d+/)) {
        	
        	def formFieldBean = triggerActionContext.getFormBean().getFieldBeanByDisplay(key);
           
        	if(null != formFieldBean) {
        		
        		key = formFieldBean.getName();
        	}
        }
        
        def value = formData.getFieldValue(key);
        
        if(null == value){
            value = formData.getAllDataMap().get(key);
        }
        
        return (null != value ? value : defaultValue);
    } catch(Exception e){
        logger("从【触发器】的环境对象中获取表单数据失败！[KEY: ${key}, ERROR: ${e.message}]");
    }
    
    return defaultValue;
}


/* 在【触发器】的环境下，对当前表单数据进行更新 */
def setFieldValue(key, value, triggerActionContext=__TRIGGER_CONTEXT__){
    
	def sqlClient = null;
	
	try {
		def formData = triggerActionContext.getMasterBean();
    	def formBean = triggerActionContext.getFormBean();
    	
    	def formFieldBean = formBean.getFieldBeanByDisplay(key);
    	
    	if(null == formFieldBean){
    		formFieldBean = formBean.getFieldBeanByName(key);
    	}
        
		if(null == formFieldBean){
			logger("${key}, 不存在！");
			return;
		}

		def id = formData.getId();
		def fieldName = formFieldBean.getName();
		def tableName = formFieldBean.getOwnerTableName();
		
		def sql = '';
		def updataSize = 0;
		
		sqlClient = new groovy.sql.Sql(com.seeyon.ctp.common.AppContext.getBean('dataSource'));
		
		if(formFieldBean.isMasterField()) { // 主表
			
			updataSize = sqlClient.executeUpdate((sql = "UPDATE ${tableName} SET ${fieldName}=? WHERE id=?"), [value, id]);
			
			if(1 != updataSize) {
				logger("在【触发器】的环境下，对当前表单数据进行更新失败！SQL: ${sql}, VALUE: ${value[i]}, ID: ${id}");
			}
			
		} else { // 从表
			
			def subFormData = formData.getSubData(tableName);
        
			if(value.size() != subFormData.size()) {
				logger("从表数据行数与传入的数据行数不匹配！【${subFormData.size()} -> ${value.size()}】");
				return;
			}
			
			def i = 0;
			
			for(sfd in subFormData) {
				
				id = sfd.id;
				
				updataSize = sqlClient.executeUpdate((sql = "UPDATE ${tableName} SET ${fieldName}=? WHERE id=?"), [value[i], id]);
				
				if(1 != updataSize) {
					logger("在【触发器】的环境下，对当前表单数据进行更新失败！SQL: ${sql}, VALUE: ${value[i]}, ID: ${id}");
				}
				
				i++;
			}
		}
		
    } catch(Exception e){
        logger("在【触发器】的环境下，对当前表单数据进行更新失败！[KEY: ${key}, ERROR: ${e.message}]");
    }finally{
		
		if(null != sqlClient){
			try{
				sqlClient.close();
			} catch(Throwable e){}
		}
	}
}


/* 在【触发器】的环境下取回协同流程 */
def takebackTriggerWorkflow(triggerActionContext=__TRIGGER_CONTEXT__){
	
	def param = triggerActionContext.getParam();
	
	if(null == param || param.isEmpty) {

		throw new snail.common.ServiceException("在【触发器】的环境下取回协同流程失败！（param为空）");
	}
	
	def moduleId = MapHelper.getLong(param, 'moduleId', -1L);
	def activityId = MapHelper.getLong(param, 'activityId', -1L);
	def memberId = MapHelper.getLong(param, 'affair_member_id', -1L);
	
	getObjBySQL('select id, subject, sub_object_id from ctp_affair where is_delete=0 and state=4 and object_id=? and activity_id=? and member_id=? order by receive_time desc', [moduleId, activityId, memberId], {
		
		try {
			
			def wfApi = AppContext.getBean("workflowInnerApiManager");
			
			def bpmParam = new com.seeyon.ctp.rest.resources.SeeyonBPMHandParam4Tackback();
			bpmParam.setAppName('collaboration');
			bpmParam.setCurrentUser(AppContext.getBean("orgManager").getMemberById(memberId));
			bpmParam.setAppData(['isSaveOpinion': 'true']);
			bpmParam.setWorkitemId(it.sub_object_id as String);
			
			def wfApiResult = wfApi.transTakeBack(bpmParam);
			
			if (!wfApiResult.isHandResult()) {
				throw new snail.common.ServiceException('在【触发器】的环境下取回协同流程失败！ERR-MSG: ' + wfApiResult.getErrorMsg() + '，ERR-CODE: ' + wfApiResult.getErrorCodeIntValue()); 
			}

			sendTriggerMessage("《${it.subject}》流程被系统退回了，请重新处理！！！");
			
		}catch(Exception e) {
			throw new snail.common.ServiceException('在【触发器】的环境下取回协同流程失败！ERR-MSG: ' + e.getMessage());
		}
		
	}, {
		
		throw new snail.common.ServiceException("在【触发器】的环境下取回协同流程失败！（未找到有效数据）");
	});
}


/* 在【触发器】的环境下退回已结束的协同流程 */
def rollbackTriggerWorkflow(triggerActionContext=__TRIGGER_CONTEXT__){
	
	def param = triggerActionContext.getParam();
	
	if(null == param || param.isEmpty) {

		throw new snail.common.ServiceException("在【触发器】的环境下退回已结束的协同流程失败！（param为空）");
	}
	
	def moduleId = MapHelper.getLong(param, 'moduleId', -1L);
	def activityId = MapHelper.getLong(param, 'activityId', -1L);
	def memberId = MapHelper.getLong(param, 'affair_member_id', -1L);
	
	getObjBySQL('select id, subject from ctp_affair where is_delete=0 and state=4 and object_id=? and activity_id=? and member_id=? order by receive_time desc', [moduleId, activityId, memberId], {
		
		try {
			
			def sqlClient = null;
			
			try {
				
				sqlClient = getSQLClient();
				
				sqlClient.executeUpdate('update ctp_affair set state=3, sub_state=11, summary_state=0, complete_time=null, is_finish=0 where id=?', [it.id]);
				sqlClient.executeUpdate('update ctp_affair set summary_state=0, is_finish=0 where object_id=?', [moduleId]);
				sqlClient.executeUpdate('update col_summary set state=2, finish_date=null, over_worktime=null, run_worktime=null, over_time=null, run_time=null where id=?', [moduleId]);
				
				sqlClient.close();
				sqlClient = null;
				
				sendTriggerMessage("《${it.subject}》流程被系统退回了，请重新处理！！！");
				
			} finally {
				
				if(null != sqlClient) {
					
					sqlClient.close();
					sqlClient = null;
				}
			}		
			
		}catch(Exception e) {
			throw new snail.common.ServiceException('在【触发器】的环境下退回已结束的协同流程失败！ERR-MSG: ' + e.getMessage());
		}
		
	}, {
		
		throw new snail.common.ServiceException("在【触发器】的环境下退回已结束的协同流程失败！（未找到有效数据）");
	});
}
